1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5
6 public class BirdScripts : MonoBehaviour {
7
8 public static BirdScripts instance;
9
10 [SerializeField]
11 public Rigidbody2D myRigidBody;
12
13 [SerializeField]
14 private Animator anim;
15
16 private float forwardSpeed = 3f;
17 private float bounceSpeed = 4f;
18
19 private bool didFlap;
20 public bool isAlive;
21
22 private Button flapButton;
23
24 [SerializeField]
25 private AudioSource audioSource;
26
27 [SerializeField]
28 private AudioClip flapClick, pointClip, diedClip, cheerClip;
29
30 public int score;
31
32 private void Awake()
33 {
34 if(instance == null)
35 {
36 instance = this;
37 }
38 isAlive = true;
39 score = 0;
40 flapButton = GameObject.FindGameObjectWithTag("FlapButton").GetComponent<Button>();
41 flapButton.onClick.AddListener(() => flapTheBird());
42
43 CameraX();
44 }
45
46 // Use this for initialization
47 void Start () {
48
49 }
50
51 // Update is called once per frame
52 void FixedUpdate () {
53 if (isAlive)
54 {
55 Vector3 temp = transform.position;
56 temp.x += forwardSpeed * Time.deltaTime;
57 transform.position = temp;
58
59 if (didFlap)
60 {
61 didFlap = false;
62 myRigidBody.velocity = new Vector2(0, bounceSpeed);
63 audioSource.PlayOneShot(flapClick);
64 anim.SetTrigger("Flap");
65 }
66
67 if(myRigidBody.velocity.y >= 0)
68 {
69 transform.rotation = Quaternion.Euler(0, 0, 0);
70 } else {
71 float angle = 0;
72 angle = Mathf.Lerp(0, -70, -myRigidBody.velocity.y / 7);
73 transform.rotation = Quaternion.Euler(0, 0, angle);
74 }
75 }
76 }
77
78 void CameraX()
79 {
80 CameraScript.offsetX = (Camera.main.transform.position.x - transform.position.x) - 1f;
81 }
82
83 public float GetPositionX() {
84 return transform.position.x;
85 }
86
87 public void flapTheBird()
88 {
89 didFlap = true;
90 }
91
92 void OnCollisionEnter2D(Collision2D target) {
93 if (target.gameObject.tag == "Pipe" || target.gameObject.tag == "Ground" || target.gameObject.tag == "Enemy") {
94 if (isAlive) {
95
96 isAlive = false;
97 anim.SetTrigger ("BirdDied");
98 audioSource.PlayOneShot (diedClip);
99 GamePlayController.instance.playerDiedShowScore (score);
100 }
101 } else if (target.gameObject.tag == "Flag") {
102 if (isAlive) {
103
104 isAlive = false;
105 audioSource.PlayOneShot (cheerClip);
106 GamePlayController.instance.finishGame();
107 }
108 }
109 }
110
111 void OnTriggerEnter2D(Collider2D target) {
112 if (target.tag == "PipeHolder") {
113 audioSource.PlayOneShot(pointClip);
114 score++;
115 GamePlayController.instance.setScore(score);
116 }
117 }
118
119 }